<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Application extends Model
{
    use HasFactory;
    protected $fillable = ['type','model','source','project_id','user_id','reason','status','source_id','price','payment_type','pay_info','pay_date','invoice_numbers','invoice_file','invoice_type','receipt','is_process','reviewer_id'];

    const POSTONLY = ['type','model','source','project_id','user_id','reason','status','source_id','price','payment_type','pay_info','pay_date','invoice_numbers','invoice_file','invoice_type','receipt','is_process','reviewer_id'];

    protected $appends = [
        'status_text'
    ];

    public function getStatusTextAttribute()
    {
        $arr = ['pending'=>'待审核','approved'=>'已通过','rejected'=>'已驳回'];
        return $arr[$this->status];
    }

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }


    public function records()
    {
        return $this->hasMany(ApplicationRecord::class,'application_id','id')->with('user');
    }

    public function records_pending()
    {
        return $this->hasMany(ApplicationRecord::class,'application_id','id')->where('status','pending')->with('user');
    }

    public function records_approved()
    {
        return $this->hasOne(ApplicationRecord::class,'application_id','id')->where('status','approved');
    }



    public function scopeUser($query, $user_id = 0)
    {
        return $query->where('user_id', $user_id);
    }

    public function project()
    {
        return $this->hasOne(Project::class,'id','project_id')->select('id','title','status');
    }

    public function user()
    {
        return $this->hasOne(User::class,'id','user_id')->select('id','name');
    }

    public function certDown()
    {
        return $this->hasOne(CertDown::class,'cert_id','source_id')->with('cert');
    }

    public function down()
    {
        return $this->hasOne(Down::class,'id','source_id');
    }

    public function contract()
    {
        return $this->hasOne(Contract::class,'id','source_id');
    }

    public function other()
    {
        return $this->hasOne(Other::class,'id','source_id');
    }

    public function diary()
    {
        return $this->hasOne(ConstructionDiary::class,'id','source_id');
    }

    public function difficulty()
    {
        return $this->hasOne(Difficulty::class,'id','source_id');
    }

    public function risk()
    {
        return $this->hasOne(Risk::class,'id','source_id');
    }

    public function projectInvoice()
    {
        return $this->hasOne(ProjectInvoice::class,'id','source_id');
    }

    public function material()
    {
        return $this->hasOne(Material::class,'id','source_id');
    }

    public function materialPay()
    {
        return $this->hasOne(Reimburse::class,'id','source_id');
    }

    public function Reimburse()
    {
        return $this->hasOne(Reimburse::class,'id','source_id')->with('details');
    }

    public function labor()
    {
        return $this->hasOne(Labor::class,'id','source_id');
    }

    public function laborPay()
    {
        return $this->hasOne(Labor::class,'id','source_id');
    }

    public function material_contract()
    {
        return $this->hasOne(MaterialContract::class,'id','source_id');
    }

    public function materialContract()
    {
        return $this->hasOne(MaterialContract::class,'id','source_id');
    }

    public function materialContrast()
    {
        return $this->hasOne(materialContrast::class,'material_id','source_id');
    }

    public function laborContrast()
    {
        return $this->hasMany(laborContrast::class,'labor_id','source_id')->with('labor');
    }

    public function laborContract()
    {
        return $this->hasMany(laborContract::class,'id','source_id');
    }

    public function cert()
    {
        return $this->hasMany(Cert::class,'id','source_id');
    }

    public function leave()
    {
        return $this->hasOne(Leave::class,'id','source_id');
    }

    public function imprest()
    {
        return $this->hasOne(Imprest::class,'id','source_id');
    }

    public function stamp()
    {
        return $this->hasOne(Stamp::class,'id','source_id');
    }

    public function WarehousesIn()
    {
        return $this->hasOne(WarehousesIn::class,'id','source_id')->with('warehouse');
    }

    public function WarehousesOut()
    {
        return $this->hasOne(WarehousesOut::class,'id','source_id')->with('warehouse');
    }

    public function supplier()
    {
        return $this->hasOne(Supplier::class,'id','source_id');
    }

    public function team()
    {
        return $this->hasOne(Team::class,'id','source_id');
    }

}
